<?php

/**
 * @author 陈韵
 * edmon创建订单
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Model\CommodityModel\SupplierCommodity;
use App\Service\Code\Normal;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\Shopedmon\ShopedmonApiClient;
use App\Service\TraitService\SupplierRouterOrderTrait;

class Shopedmon extends BaseSupplierRouterService
{
    use SupplierRouterOrderTrait;
    
    /**
     * 订单申报
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopedmonApiClient::class,$param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopedmonApiClient::class,$param['apiBody'])->cancelOrder($param);
    }
    
    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        $page   = $param['queryBody']['page'] ?? 1;
        $limit  = $param['queryBody']['limit'] ?? 20;
        
        //更新商品资料,所以直接根据供应商skuCode获取商品
        $goodsData = SupplierCommodity::instance()
        ->where('SupplierCompanyID',$param['queryBody']['supplierID'])
        ->orderBy('ID','asc')
        ->paginate($limit, ['SupplierCommoditySkuCode'], 'page', $page)->toArray();
        
        //没有数据就说明请求到最后一页
        if (empty($goodsData['data'])) {
            return Normal::returnCode(Normal::SUC,"获取成功",[
                'total' =>  $goodsData['total'],
                'list'  =>  []
            ]);
        }
        
        $param['queryBody']['skuNos'] = array_column($goodsData['data'],'SupplierCommoditySkuCode');
        $res = $this->callSdkApi(ShopedmonApiClient::class,$param['apiBody'])->queryGoodsListBySkuNo($param['queryBody']);
        if(empty($res)){
            return Normal::returnCode(Normal::FAIL,"請求失敗");
        }
        
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total' => count($res) ?? 0,
            'list'  => $res,
        ]);
    }
}